import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import ArticleList from '../views/ArticleList.vue'
import AddArticleList from '../views/AddArticleList.vue'
import EditArticleList from '../views/EditArticleList.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    children: [{
        path: 'articleList',
        name: 'articleList',
        component: ArticleList,
    }, {
        path: 'addArticleList',
        name: 'addArticleList',
        component: AddArticleList,
    }, {
        path: 'editArticleList',
        name: 'editArticleList',
        component: EditArticleList,
    }],
}, {
    path: '/login',
    name: 'login',
    component: Login
}]


const router = new VueRouter({
    routes
})

// 添加全局守卫
router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        return next();
    } else if (!localStorage.getItem('token')) {
        return router.push('/login').catch((err) => {});
    }
    return next();
})

export default router